$(function () {
    $("#jqGrid").jqGrid({
        url: '../news/list',
        datatype: "json",
        colModel: [
            {label: 'id', name: 'id', index: 'id', key: true, hidden: true, align: "center"},
            {label: '资讯名称', name: 'name', index: 'name', width: 80, align: "center"},
            {label: '作者', name: 'author', index: 'author', width: 80, align: "center"},
            {
                label: '封面图', name: 'coverPicUrl', index: 'cover_pic_url', width: 80, align: "center",
                formatter: function (value) {
                    return transImg(value);
                }
            },
            // {label: '详细内容', name: 'contentDesc', index: 'content_desc', width: 80, align: "center"},
            {
                label: '类型', name: 'type', index: 'type', width: 80, align: "center", formatter: function (value) {
                    if (value != null) {
                        if (value === 1) {
                            return '<span class="label label-info">新闻快讯</span>';
                        } else if (value === 2) {
                            return '<span class="label label-success">活动资讯</span>';
                        } else {
                            return '<span class="label label-warning">健康分享</span>';
                        }
                    }
                }
            },
            {label: '浏览数', name: 'viewCount', index: 'viewCount', width: 80, align: "center"},
            {
                label: '创建时间', name: 'addTime', index: 'add_time', width: 80, align: "center",
                formatter: function (value) {
                    return transDate(value, 'yyyy-MM-dd hh:mm:ss');
                }
            },
            {
                label: '修改时间', name: 'updateTime', index: 'update_time', width: 80, align: "center",
                formatter: function (value) {
                    return transDate(value, 'yyyy-MM-dd hh:mm:ss');
                }
            }],
        viewrecords: true,
        height: 495,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
    $('#contentDesc').editable({
        inlineMode: false,
        alwaysBlank: true,
        height: '500px', //高度
        minHeight: '200px',
        language: "zh_cn",
        spellcheck: false,
        plainPaste: true,
        enableScript: false,
        imageButtons: ["floatImageLeft", "floatImageNone", "floatImageRight", "linkImage", "replaceImage", "removeImage"],
        allowedImageTypes: ["jpeg", "jpg", "png", "gif"],
        imageUploadURL: '../sys/oss/upload',
        imageUploadParams: {id: "edit"},
        imagesLoadURL: '../sys/oss/queryAll'
    })
});

let vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        news: {
            coverPicUrl: '',
        },
        ruleValidate: {
            name: [
                {required: true, message: '名称不能为空', trigger: 'blur'},
                {max: 80, message: '请输入最多80位'}
            ],
            author: [
                {required: true, message: '作者不能为空', trigger: 'blur'},
                {max: 10, message: '请输入最多10位'}
            ],
            type: [
                {required: true, message: '类型不能为空', trigger: 'blur', type: 'number'},
            ],
            coverPicUrl: [
                {required: true, message: '封面图不能为空', trigger: 'blur'}
            ]
        },
        q: {
            name: ''
        }
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.news = {
                coverPicUrl: '',
            };
            $('#contentDesc').editable('setHTML', '');
        },
        update: function (event) {
            let id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            let url = vm.news.id == null ? "../news/save" : "../news/update";
            vm.news.contentDesc = $('#contentDesc').editable('getHTML');
            if (vm.news.contentDesc == null || vm.news.contentDesc === '') {
                alert('请填写详细内容');
                return;
            }

            $.ajax({
                type: "POST",
                url: url,
                contentType: "application/json;charset=utf-8",
                data: JSON.stringify(vm.news),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            let ids = getSelectedRows();
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: "../news/delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function (id) {
            $.get("../news/info/" + id, function (r) {
                vm.news = r.news;
                $('#contentDesc').editable('setHTML', vm.news.contentDesc);
            });
        },
        reload: function (event) {
            vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name, 'type': vm.q.type},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleSuccessPicUrl: function (res, file) {
            vm.news.coverPicUrl = file.response.url;
        },
        handleFormatError: function (file) {
            this.$Notice.warning({
                title: '文件格式不正确',
                desc: '文件 ' + file.name + ' 格式不正确，请上传 jpg 或 png 格式的图片。'
            });
        },
        handleMaxSize: function (file) {
            this.$Notice.warning({
                title: '超出文件大小限制',
                desc: '文件 ' + file.name + ' 太大，不能超过 2M。'
            });
        },
        eyeImagePicUrl: function () {
            var url = vm.news.coverPicUrl;
            eyeImage(url);
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        }
    }
});